precision mediump float;
uniform vec2 u_resolution;
uniform float u_time;
uniform vec2 u_mouse;
#define SM(a,b,c)smoothstep(a,b,c);
//随机数
float random(float t){
  return fract(sin(t*17.1541)*
43758.5453123);
}
//旋转矩阵
mat2 getRoationMatrix(float angle){
float s=sin(angle);
float c=cos(angle);
return mat2(c,-s,s,c);
}
//插值函数
float blurFun(float t,float a,float b){
//return clamp(mix(t,b,a),0.,1.) ;

return clamp((t-a)/(b-a),0.,1.);

}
float band(float t,float start,float end,float blur){
float d1=smoothstep(start-blur,start+blur,t);
float d2=smoothstep(end+blur,end-blur,t);
return d1*d2;

}
float rect(float x,float y,float startx,float endx,float starty,float endy,float blur){
float rect1=band(x,startx,endx,blur);
float rect2=band(y,starty,endy,blur);
return rect1*rect2;

}
vec3 head(vec2 uv,float r){

//绘制大脸蛋子
vec3 col=vec3(0.);
float d=length(uv);
float c=SM(r+.01,r,d);
col=mix(col,vec3(.9,.65,.1),c);

//绘制大脸蛋子边缘
float dout=length(uv);
float inthuban=SM(r-.09,r-.01,dout);
float outhuban=SM(r-.01,r+.02,dout);
float cout=inthuban-outhuban;
col=mix(col,vec3(.6,.3,.1),cout);

//额头
float etoud=length(uv);
uv.y=uv.y+uv.x*uv.x;//此刻uv.y等于= -uv.x*uv.x时等号左边等于0.
float hightlight=SM(r-.12,r-.13,etoud);
hightlight*=blurFun(uv.y,0.,r-.32);
col=mix(col,vec3(1.),hightlight*.5);

return col;
}

vec3 eye(vec2 uv,float r,vec3 col,vec2 mouse){
uv.x=abs(uv.x);
//白色眼仁
uv-=vec2(.34,.2);
vec2 oruv=uv;//保存原始的uv坐标不受侵害
uv*=getRoationMatrix(radians(-20.));
uv.x*=1.5;//x方向变瘦
float outeye=length(uv);
float c=SM(r+.01,r,outeye);
col=mix(col,vec3(1.),c);

//绘制眼眶
float yankuangout=length(uv);
float yankuangsm1=SM(.35,.33,yankuangout);
float yankuangsm2=SM(.33,.32,yankuangout);
float yankuangsm=yankuangsm1-yankuangsm2;
col=mix(col,vec3(0.),yankuangsm);

//绘制眼影
uv-=vec2(0.,.01);
float yanying=length(uv);
uv-=vec2(0.,.05);
float yanying1=length(uv);
uv.y=uv.x*uv.x;
float yanlight1=SM(.3,.28,yanying1);
float yanlight2=SM(.31,.29,yanying);
float yanlight=yanlight1-yanlight2;
col=mix(col,vec3(.1922,.1922,.1922),yanlight*.3);

//绘制外围瞳孔
oruv*=getRoationMatrix(radians(-10.));
oruv-=vec2(mouse.x*.01,mouse.y*.01);
oruv.x*=1.5;
float tongkongoutd=length(oruv);
float tongkongoutsm=SM(.25,.23,tongkongoutd);
col=mix(col,vec3(.0,.29,.6),tongkongoutsm);

//绘制中间瞳孔
oruv+=vec2(.04,.02);
oruv*=1.5;
float tongkongmiddled=length(oruv);
float tongkongoutmiddlesm=SM(.25,.23,tongkongmiddled);
col=mix(col,vec3(.0,.62,1.),tongkongoutmiddlesm);

//绘制小瞳孔
oruv+=vec2(.04,.02);
oruv.x/=mix(1.3,1.5,sin(u_time));//瞳孔随时间放大与缩小
float tongkongsmalld=length(oruv);
float tongkongsmallsm=SM(.12,.11,tongkongsmalld);
col=mix(col,vec3(0.),tongkongsmallsm);

//绘制眼睛里的光斑
vec2 offs=vec2(sin(u_time+oruv.y*15.),cos(u_time+oruv.x*10.))*.05;
oruv-=vec2(.04)+offs;
float tongkongbrightd1=length(oruv);
float tongkongbrightd2=length(oruv+vec2(.06));
float tongkongbrightd1sm=SM(.02,.01,tongkongbrightd1);
float tongkongbrightd2sm=SM(.021,.02,tongkongbrightd2);
float tongkongbrightdsm=tongkongbrightd1sm+tongkongbrightd2sm;
col=mix(col,vec3(1.),tongkongbrightdsm);
return col;
}
vec3 eyebrow(vec2 uv,vec3 col){
vec2 oruv=uv;
uv.x=abs(uv.x);
//绘制眼眉
uv.y+=uv.x*uv.x*mix(.6,.4,sin(u_time));
uv-=vec2(.35,.39);
float yanmei=length(uv);
uv-=vec2(0.,.07);
float yanmei1=length(uv);
float yanmeilight=smoothstep(.25,.23,yanmei1)-smoothstep(.25,.23,yanmei);
yanmeilight=clamp(yanmeilight,0.,1.);

col=mix(col,vec3(0.),yanmeilight*.7);

//绘制眼底
oruv.x=abs(oruv.x);
oruv-=vec2(.42,-.11);
oruv.x*=mix(1.8,2.,sin(u_time));
oruv.y+=oruv.x*oruv.x*mix(.6,.4,sin(u_time));
oruv.y*=14.;
float yandi=length(oruv);
float yandilight=smoothstep(.31,.3,yandi);
col=mix(col,vec3(0.),yandilight*.5);
return col;

}
vec3 mouth(vec2 uv,vec3 col){
//绘制大嘴边缘
vec2 oruv=uv;
uv+=vec2(0.,.5);
uv.x/=mix(1.5,2.,sin(u_time));
uv.y-=uv.x*uv.x*mix(0.,1.,sin(u_time));
uv.y*=2.;
float zuiba=length(uv);
float zuibalight=smoothstep(.31,.3,zuiba);
col=mix(col,vec3(.21),zuibalight);

//绘制嘴唇
float zuichun=smoothstep(.31,.3,zuiba)-smoothstep(.29,.28,zuiba);
col=mix(col,vec3(.3843,.2667,.2667),zuichun);
//绘制牙齿
vec3 yachi=vec3(rect(oruv.x,oruv.y,-.14,0.,-.45,uv.x*uv.x*mix(0.,1.,sin(u_time))-.35,.01)+rect(oruv.x,oruv.y,.01,.15,-.45,uv.x*uv.x*mix(0.,1.,sin(u_time))-.35,.01));
col=mix(col,vec3(1.),yachi);

//绘制舌头
oruv+=vec2(0.,.56);
oruv.x*=mix(.45,.3,sin(u_time));

float shetou=length(oruv);
float shetoulight=smoothstep(.09,.08,shetou);
col=mix(col,vec3(1.,0.,.1),shetoulight);

return col;

}
vec3 dimple(vec2 uv,vec3 col){
//保存原始uv 绘制左右两侧的酒窝
vec2 oruv=uv;
uv.x=abs(uv.x);
vec2 move=mix(vec2(.5,-.45),vec2(1.,-.3),sin(u_time)*.3);
uv-=move;
uv=uv*getRoationMatrix(radians(70.));
uv*=2.;
uv.x*=mix(2.,1.5,sin(u_time));
uv.y-=uv.x*uv.x*2.;
uv.y*=8.;
float jiuwo=length(uv);
float jiuwolight=smoothstep(.31,.2,jiuwo);
col=mix(col,vec3(.25),jiuwolight);

//绘制下巴上的酒窝

oruv*=2.;
oruv.x*=2.;
oruv.y+=1.4;
oruv.y+=oruv.x*oruv.x*0.5;
oruv.y*=15.;
float jiuwo1 = length(oruv);
float jiuwolight1 = smoothstep(0.31,0.3,jiuwo1);
col = mix(col,vec3(0.25),jiuwolight1);



return col;

}
void main(void)
{

vec2 uv=(gl_FragCoord.xy*2.-u_resolution.xy)/min(u_resolution.x,u_resolution.y);//uv坐标标准化
vec2 mouse=(u_mouse.xy*2.-u_resolution.xy)/min(u_resolution.x,u_resolution.y);//mouse坐标标准化
vec3 headcol=head(uv,.9);
vec3 eyecol=eye(uv,.33,headcol,mouse);
vec3 eyebrowcol=eyebrow(uv,eyecol);
vec3 mouthcol=mouth(uv,eyebrowcol);
vec3 dimplecol=dimple(uv,mouthcol);
gl_FragColor=vec4(dimplecol,1.);
}